package tcu.iot.gasmanage.api;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import tcu.iot.gasmanage.common.Result;
import tcu.iot.gasmanage.common.utils.QuerySwitchUtils;
import tcu.iot.gasmanage.entity.equipment.Equipment;
import tcu.iot.gasmanage.entity.equipment.EquipmentQuery;
import tcu.iot.gasmanage.service.EquipmentService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(value = "设备管理控制器")
@RestController
@ResponseBody
@RequestMapping("/equipment")
@CrossOrigin
public class EquipmentController {

    @Resource
    private EquipmentService equipmentService;

    @ApiOperation(value = "/list", notes = "获取所有设备信息数据")
    @GetMapping(value = "/list")
    public Result getAllEquipment(HttpServletRequest request) {
        EquipmentQuery equipmentQuery = JSON.parseObject(QuerySwitchUtils.QuerySwitch(request.getQueryString()), EquipmentQuery.class);
        return equipmentService.getAllEquipment(equipmentQuery);
    }

    @ApiOperation(value = "/options", notes = "获取所有选项")
    @PostMapping(value = "/options")
    public Result getOptions() {
        return equipmentService.getOptions();
    }

    @ApiOperation(value = "/delete", notes = "删除设备信息")
    @PostMapping(value = "/delete")
    public Result DeleteEquipment(@Param(value = "id") int id) {
        return equipmentService.DeleteEquipment(id);
    }

    @ApiOperation(value = "/create", notes = "添加设备信息")
    @PostMapping(value = "/create")
    public Result CreateEquipment(@RequestBody Equipment equipment) {
        if (equipment.getEquipmentNumber().equals("bd1631436742312")) {
            equipment.setElectricQuantity(0);
            equipment.setSignalStrength(0);
        } else {
            equipment.setElectricQuantity(0);
            equipment.setSignalStrength(-1);
        }
        return equipmentService.InsertEquipment(equipment);
    }

    @ApiOperation(value = "更新设备数据", notes = "修改设备信息")
    @PostMapping(value = "/update")
    public Result UpdateEquipment(@RequestBody Equipment equipment) {
        return equipmentService.UpdateEquipment(equipment);
    }

    @ApiOperation(value = "获得所有已经存在的设备列表", notes = "获得所有已经存在的设备列表")
    @GetMapping(value = "/getTrueEquipment")
    public Result GetTrueEquipment() {
        return equipmentService.GetTrueEquipment();
    }
}
